// component/common/com-pyq-pop/com-pyq-pop.js
import api from "../../../common/api.js";
import constant from "../../../common/constant.js";
import util from "../../../utils/util.js";

const _map = {
  NEWHOUSE: constant.XIN_FANG,
  RENT: constant.CHU_ZU,
  SALES: constant.ER_SHOU,
};

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    model: {
      type: Object,
      value: {},
    },
    houseTypes: {
      type: String,
      value: "SALES"
    }
  },

  options: {
    styleIsolation: "apply-shared",
  },

  /**
   * 组件的初始数据
   */
  data: {},

  /**
   * 组件的方法列表
   */
  methods: {
    handleClick() {},
    friendsShareTabDialog() {
      this.triggerEvent("onHide");
    },

    // 进入对话框分享
    enterConversation() {
      this.triggerEvent("onHide");
      console.log('=====  ', this.data.houseTypes);

      if (this.data.houseTypes == "NEWHOUSE") {
        this.enterConversationNewHouse();
        return;
      }

      let loginUserInfo = getApp().globalData.loginUserInfo;
      let userid = loginUserInfo.data.content.sessionUser.id;

      let { hmDelSummaryInfo, houDelVO, houImageVO } = this.data.model;
      let isVr = hmDelSummaryInfo.houSubType == 1 && hmDelSummaryInfo.houSubTypeState == 10 ? "VR看房 " : "";
      let _price =
        houDelVO.totalPrices && houDelVO.totalPrices != -1 && houDelVO.totalPrices != -0.01
          ? houDelVO.delegationType == "SALES"
            ? houDelVO.totalPrices / 10000 + "万元"
            : houDelVO.totalPrices + "元/月"
          : "未填价格";
      let _size = houDelVO.buildSize != -1 && houDelVO.buildSize ? houDelVO.buildSize + "㎡" : "未填面积";
      let copyText = isVr + houDelVO.resblockName + _price + _size;
      console.log('分享的H5链接。。。。', `${util.shareUrl}/noauth/hou/RentalHouseDetail?houDelId=${houDelVO.id}&userId=${userid}`)
      let message = {
        touser: wx.getStorageSync("aldstat_op"),
        msgtype: "link",
        link: {
          title: `【${copyText}】分享链接`,
          description: "点击可以分享至朋友圈",
          url: `${util.shareUrl}/noauth/hou/RentalHouseDetail?houDelId=${houDelVO.id}&userId=${userid}`,
          thumb_url: houImageVO.length > 0 ? houImageVO[0].url : "/images/defaultImgSmall.jpg",
        },
      };

      api.setHouseInfo(
        {
          agentid: userid,
          openid: wx.getStorageSync("aldstat_op"),
          message: JSON.stringify(message),
        },
        (res) => {
          wx.gio("track", constant.SHARE_SUCCESS, {
            [constant.ROOMTYPE_VAR]: _map[houDelVO.delegationType],
            [constant.SHARETYPE_VAR]: constant.PYQ_SHARE,
          });
        }
      );
    },

    // 新房的朋友圈分享
    enterConversationNewHouse() {
      let detailContent = this.data.model;
      var loginUserInfo = getApp().globalData.loginUserInfo;
      let userid = loginUserInfo.data.content.sessionUser.id;

      let message = {
        touser: wx.getStorageSync("aldstat_op"),
        msgtype: "link",
        link: {
          title: detailContent.title + "分享链接",
          description: "点击可以分享至朋友圈",
          url: `${util.shareUrl}/noauth/hou/houseNewDetail?houDelId=${detailContent.id}&userId=${userid}`,
          thumb_url: `${detailContent.first_picture_url}`,
        },
      };

      api.setHouseInfo(
        {
          agentid: userid,
          openid: wx.getStorageSync("aldstat_op"),
          message: JSON.stringify(message),
        },
        (res) => {
          wx.gio("track", constant.SHARE_SUCCESS, {
            [constant.ROOMTYPE_VAR]: _map[this.data.houseTypes],
            [constant.SHARETYPE_VAR]: constant.PYQ_SHARE,
          });
        }
      );
    },
  },
});
